/**
 *  Copyright (C) 2010:
 *                 Buzzanca Marco <marco.bzn@gmail.com>
 *         Monaco Davide Giuseppe <black.ralkass@gmail.com>
 *              Petralia Veronica <veronicapetralia@gmail.com>
 *
 * This file is part of DS2011BMP
 *
 * DS2011BMP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DS2011BMP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/**
 * @file worker.h
 * @author Marco Buzzanca
 * @author Davide Giuseppe Monaco
 *
 * This file contains Worker class declaration.
 */

#ifndef _WORKER_H_
#define _WORKER_H_ 1

#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

#include <unistd.h>

#include "address.h"
#include "workflow.h"
#include "tqueuemanager.h"
#include "udpserver.h"
#include "uiproxyfactory.h"
#include "uiproxy.h"

/**
 * @brief Enumeration containing possible Worker status.
 */
enum WorkerStatus
{
    Free, Working
};

class Worker
{
private:
  /**
   * @brief Current Worker status according to WorkerStatus enumeration.
   */
  WorkerStatus _status;

  /**
   * @brief The TaskDescriptor queue manager.
   */
  TQueueManager _tqm;

  /**
   * @brief Proxy used to communicate with UI.
   */
  UIProxyFactory _uipf;
  
  /**
   * @brief UDP Server used to receive files sent by UI.
   */
  UDPServer _file_server;

  /**
   * @brief worker thread group.
   */
  boost::thread_group _workers;

  /**
   * @brief worker thread callback member function.
   */
  void serve(int id);

public:
  /**
   * @brief Default constructor
   *
   * TODO: check following default port, is it 14000 or 15000 ?
   */
  Worker(boost::uint16_t port = 14000);

  /**
   * @brief Destructor
   */
  ~Worker();

  /**
   * @brief Execute the operation specified in the provided TaskDescriptor.
   * @param val The provided TaskDescriptor.
   */
  int exec(const TaskDescriptor& val);

  /**
   * @brief Start the TaskDescriptor queue manager and worker threads. 
   */
  void run(void);
};

#endif /*_WORKER_H_*/
